package mr.ali.video.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import mr.ali.video.web.mapper.CollectionPlayMapper;
import mr.ali.video.web.mapper.CollectionPlayUrlMapper;
import mr.ali.video.web.mapper.CollectionUrlMapper;
import mr.ali.video.web.model.CollectionPlay;
import mr.ali.video.web.model.CollectionPlayUrl;
import mr.ali.video.web.service.ICollectionPlayService;
import mr.ali.video.web.service.ICollectionPlayUrlService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-03
 */
@Service
@Slf4j
public class CollectionPlayServiceImpl extends ServiceImpl<CollectionPlayMapper, CollectionPlay> implements ICollectionPlayService {

    @Resource
    private CollectionPlayMapper collectionPlayMapper;

    @Resource
    private CollectionPlayUrlMapper collectionPlayUrlMapper;

    @Resource
    private ICollectionPlayUrlService collectionPlayUrlService;

    @Override
    public PageInfo<CollectionPlay> getCollectionPlay(Integer pageNum, Integer pageSize) {
        QueryWrapper<CollectionPlay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        PageHelper.startPage(pageNum, pageSize);
        List<CollectionPlay> collections = collectionPlayMapper.selectList(queryWrapper);
        for (CollectionPlay collection : collections) {
            Integer row = collectionPlayUrlMapper.CountByUrlVideoId(collection.getId());
            collection.setEpisodes(row);
        }
        return new PageInfo<>(collections);

    }

    @Override
    public Integer updateDisplayed(Integer id, Integer displayed) {
        return collectionPlayMapper.updateDisplayed(id, displayed);
    }

    @Override
    public Integer deleteById(Integer id) {
        return collectionPlayMapper.deleteById(id);
    }

    @Override
    public CollectionPlay selectById(Integer id) {
        List<CollectionPlayUrl> collectionUrls = collectionPlayUrlService.selectByCollectionId(id);
        CollectionPlay collection = collectionPlayMapper.selectById(id);
        collection.setCollectionUrls(collectionUrls);
        Integer row = collectionPlayUrlMapper.CountByUrlVideoId(collection.getId());
        collection.setEpisodes(row);
        return collection;
    }


    @Override
    public Integer insert(CollectionPlay collection) {
        log.info("collection:{}", collection);
        Integer rows = collectionPlayMapper.insert(collection);
        return rows;
    }

}
